package com.jishulianmeng.service.impl;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;

import com.jishulianmeng.dao.IGenericDao;
import com.jishulianmeng.service.IGenericService;

public class GenericService<T,PK extends Serializable> implements IGenericService<T, PK> {

	private IGenericDao<T, Serializable> dao;
	
	public void setDao(IGenericDao<T, Serializable> dao) {
		this.dao = dao;
	}

	@Override
	public void add(T entity) {
		dao.add(entity);		
	}

	@Override
	public void del(Serializable id) {
		dao.del(id);
	}

	@Override
	public List<Object> doQuery(String sql) {
		return dao.selectBySql(sql, 0, 0);
	}

	@Override
	public List<T> searchPage(String hql, int from, int max) {
		 return dao.searchPage(hql, from, max);
	}

	@Override
	public void update(T entity) {
		dao.update(entity);
	}

	@Override
	public T getObj(Serializable id) {
		return dao.getById(id);
	}
	@Override
	public int getTotalNumBySql(String str) {
		List list = dao.selectBySql(str, 0, 0);
		Object obj = (Object)list.get(0);
		return ((BigInteger)obj).intValue();
	}
	
	@Override
	public List<Object> searchSQLPage(String sql, int from, int max) {
		 return dao.selectBySql(sql, from, max);
	}
	
	@Override
	public Object executeMySQL(final String sql){
		return dao.executeMySQL(sql);
	}
	
	@Override
	public void addBatch(List<T> entitys){
		for(int i=0;i<entitys.size();i++){
			dao.save(entitys.get(i));
		}
	}

	@Override
	public List<Object> searchBySql(String sql) {
		// TODO Auto-generated method stub
		return dao.searchBySql(sql);
	}
}
